package com.love.marriage.config;

import com.love.marriage.common.handler.RedisKeyExpirationListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * description:  redis消息监听配置
 * author:       majf
 * createDate:   2024/10/28 10:02
 * version:      1.0.0
 */
@Configuration
public class RedisListenerConfig {

    @Autowired
    private RedisKeyExpirationListener redisKeyExpirationListener;

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);

        // 监听数据库0的过期事件
        container.addMessageListener(new MessageListenerAdapter(redisKeyExpirationListener), new PatternTopic("__keyevent@0__:expired"));

        return container;
    }
}
